package com.wawi.utils;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.*;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.Notification;

/**
 * @category 极光推送
 * @author 龚亮
 * @date 2016-05-18 14:03:21
 */
public class JPushClientUtil {

	protected static final Logger LOG = LoggerFactory.getLogger(JPushClientUtil.class);

	private static final String appKey = "b498e45d37394efb221b92b2";
	private static final String masterSecret = "cd0d274aa6fc1147a01ff33b";

	private static JPushClient jpushClient=null;
	private static JPushClient jpushClientConfig=null;
	
	
	public static JPushClient instanceJPushClient(){
		if(jpushClient==null)
			jpushClient = new JPushClient(masterSecret, appKey);
		return jpushClient;
	}
	public static JPushClient instanceJPushClientConfig(){
		//HttpProxy proxy=new HttpProxy("localhost",80,"username","password");
		ClientConfig config = ClientConfig.getInstance();
		config.setPushHostName("https://api.jpush.cn");
		if(jpushClientConfig==null)
			jpushClientConfig = new JPushClient(masterSecret, appKey,null,config);
		return jpushClientConfig;
	}
	/**
	 * 推送对象：所有平台，所有设备，内容为 content 的通知。
	 * content: 推送内容
	 */
	public static void jPush_all(String content) {
		PushPayload payload=PushPayload.alertAll(content);
		sendPush(payload);
	}

	/**
	 * 推送对象：所有平台，推送目标是别名为 alias，内容为 content 的通知。
	 * content: 推送内容
	 *   alias： 设备别名
	 */
	public static void jPush_all_alias(String alias,String content) {
		PushPayload payload=PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias))
				            .setNotification(Notification.alert(content)).build();
		sendPush(payload);
	}

	/**
	 * 推送对象：平台是 Android，推送目标是标签为 tag，内容为 content，标题为title 的通知。
	 * content: 推送内容
	 * title: 标题
	 * tag: 设备标签
	 */
	public static void jPush_all_android_tag(String title,String content,Map<String,String> extras,String... tag) {
		PushPayload payload=PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag(tag))
				.setNotification(Notification.android(title, content, extras)).build();
		sendPush(payload);
	}
    

	/**
	 * 推送对象：平台是 Andorid 与 iOS，推送目标是("tag1" 与 "tag2" 的并集)且("alias1" 与 "alias2"
	 * 的并集)， 推送内容是 - content 的消息，并且附加字段 from = JPush。
	 */
	public static void jPush_android_ios(String content) {
		PushPayload payload= PushPayload.newBuilder().setPlatform(Platform.android_ios())
				.setAudience(Audience.newBuilder().addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
						.addAudienceTarget(AudienceTarget.alias("alias1", "alias2")).build())
				.setMessage(Message.newBuilder().setMsgContent(content).addExtra("from", "极光推送").build()).build();
		sendPush(payload);
	}
	/**
	 * 发送极光推送
	 */
	public static void sendPush(PushPayload payload){
		try {
			PushResult result = instanceJPushClient().sendPush(payload);
			LOG.info("推送结果 - " + result);
		}catch (APIConnectionException e){
			LOG.error("连接失败...! ", e);
		} catch (APIRequestException e) {
			LOG.error("推送发生错误...! ", e);
			LOG.info("Error Message: " + e.getMessage());
		}
	}
}
